import sha256 from 'crypto-js/sha256';
export default {
    data(){
        return {
            modal1:false,
            module:'users',
            list: [],
            filter:{
                page:1,
                total:0,
                limit:5,
                name:'',
                cateId:''
            }
        }
    },
    methods: {
        show (formData) {
            var index = formData._id;
            this.modal1=true;           
            this.formValidate=formData;
            // console.log(this.formValidate)  ;
        },
        remove (params) {
           this.$Modal.confirm({
                title: '确认删除数据吗?',
                content: '请确认是否删除该记录！',
                onOk: () => {
                    this.axios({
                        method:'delete',
                        url:`http://localhost:3000/${this.module}/data/${params.row._id}`
                    }).then(res=>{
                        this.getData();
                    })
                }
            });
        },
        getData(){
            // console.log(this.filter)
            this.axios({
                method:'post',
                url:`http://localhost:3000/${this.module}/list`,
                data: this.filter
            }).then((response) => {
                // console.log(response.data)
                this.list = response.data.rows;
                this.filter.total = response.data.total;
                this.filter.limit = response.data.limit;
            })
        },
        ok () {
            this.$Message.info('Clicked ok');
        },
        cancel () {
            this.$Message.info('Clicked cancel');
        },
        handleSubmit (name) {
            var _id=this.formValidate._id;
            // console.log(_id);
            if(_id!=undefined){
                this.axios({
                        method:'put',
                        url:`http://localhost:3000/${this.module}/data/${_id}`,
                        data:this.formValidate
                    }).then(res=>{
                        // console.log(this.formValidate._id);
                        this.formValidate._id=undefined;
                        this.$Message.success('Success!');
                        this.modal1 = false;
                        this.$refs[name].resetFields();
                        this.getData();  
                    })
            }else{
                this.$refs[name].validate((valid) => {             
                    if (valid) {
                        console.log(this.formValidate);
                        this.formValidate.password=sha256(this.formValidate.password).toString();
                        this.axios({
                            method:'post',
                            url:`http://localhost:3000/${this.module}/data`,
                            data:this.formValidate
                        }).then(res=>{
                            this.$Message.success('Success!');
                            this.modal1 = false;
                            this.getData();
                            this.$refs[name].resetFields();
                        })
                    } else {
                        this.$Message.error('Fail!');
                    }
                })
            }
        },
        selremove(){
            var ids=this.selectdata;
            // console.log(ids);
                this.$Modal.confirm({
                    title: '确认删除数据吗?',
                    content: '请确认是否删除该记录！',
                    onOk: () => {
                        for(var i=0;i<ids.length;i++){
                            this.axios({
                                method:'delete',
                                url:`http://localhost:3000/${this.module}/data/`+ids[i]
                            }).then(res=>{
                                this.getData();
                            })
                        }
                    }
                })    
        },
        handleReset (name) {
            this.$refs[name].resetFields();
        },
        onPageChange(page){
            this.filter.page = page;
            this.getData();
        },
        onPageSizeChange(limit){
            this.filter.limit = limit;
            this.getData();
        },
        search(){
           this.getData();
        },
        onSelect(row){
            if(row.length>0){
                var ids = [];
                row.forEach(function(item,idx){
                    ids.push(item._id)
                })
            }
            this.selectdata=ids;
            // console.log(ids);
            console.log(row);
        }
    },
    mounted(){
        this.getData();
    }
}